package com.c.ctgproject.adapter.guang

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseEmptyRecyclerViewAdapter
import com.c.ctgproject.base.BaseEmptyRecyclerViewAdapterV2
import com.c.ctgproject.base.BaseRecyclerViewAdapter
import com.c.ctgproject.entity.guang.PartInWarehouseEntity
import com.c.ctgproject.ui.collect.*
import com.c.ctgproject.utils.MyConstants
import com.c.ctgproject.utils.getGson
import com.c.ctgproject.utils.handlerNull
import com.c.ctgproject.utils.noDataHandler
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.textColorResource

//type 1:零部件-散件录入-我的录入  isEditable为1可以重新编辑
class PartInWarehouseAdapter(private val context: Context, private val mList: ArrayList<PartInWarehouseEntity.DataBean.ListBean>, private val justShow: Boolean =false, private val type:Int?=null,private val mViewModel:AllPartViewModel?,private val listener:PartInWarehouseCallBack) : BaseRecyclerViewAdapter(context, mList) {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_part_in_wharehouse_list, parent, false)
        return MyViewHolder(view)
    }

    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            "订单编号:${data.orderNo.noDataHandler()}".also { holder.mOrderNumberTV.text = it }
            holder.mNameTV.text = data.createPersonName.noDataHandler()
            holder.mTimeTV.text = data.createTime.noDataHandler()
            holder.mPartTV.text = data.partsName.noDataHandler()
            holder.mPartCountTV.text = data.partsCount.noDataHandler()
            holder.mPartFromTV.text = data.partsSource.noDataHandler()
            if(mViewModel!=null){
                if(mViewModel?.searchMine?.value==true){
                    holder.mCheckBox.visibility=View.GONE
                }else{
                    holder.mCheckBox.visibility=View.VISIBLE
                }
            }
            holder.mCheckBox.isChecked = data.checked
            holder.mCheckBox.setOnClickListener {
                if(holder.mCheckBox.isChecked){
                    mViewModel?.addCarId(data.orderId)
                }else{
                    mViewModel?.removeCarId(data.orderId)
                }
//                mList[position].checked = holder.mCheckBox.isChecked
//                cListener.onWeightChecked(position,holder.mCheckBox.isChecked)
//                notifyItemChanged(position)
            }
            if (data.poundId != null) {
//                holder.mStatueTV.text = "已完成"
//                holder.mStatueTV.textColorResource = R.color.colorAccent
                holder.mView.visibility = View.GONE
                holder.mHandlerBTN.visibility = View.GONE
            } else {
//                holder.mStatueTV.text = "未完成"
//                holder.mStatueTV.textColorResource = R.color.red
                holder.mView.visibility = View.VISIBLE
                if (justShow){
                    //我的录入里面如果isEditable为1 可以编辑的
                        if (type==1&&data.isEditable==1){
                            holder.mHandlerBTN.visibility = View.VISIBLE
                            holder.mHandlerBTN.text="编辑"
                        }else{
                            holder.mHandlerBTN.visibility = View.GONE
                        }



                }else{
                    holder.mHandlerBTN.visibility = View.VISIBLE
                    holder.mHandlerBTN.text="过磅"
                }
            }

//            when(data.pound){
//                1->{
//                    holder.mStatueTV.text="已完成"
//                    holder.mStatueTV.textColorResource=R.color.colorAccent
//                    holder.mView.visibility=View.GONE
//                    holder.mFinishRL.visibility=View.GONE
//
//                }
//                0->{
//                    holder.mStatueTV.text="未完成"
//                    holder.mStatueTV.textColorResource=R.color.red
//                    holder.mView.visibility=View.VISIBLE
//                    holder.mFinishRL.visibility=View.VISIBLE
//                }
//                else->{
//                    holder.mView.visibility=View.VISIBLE
//                    holder.mFinishRL.visibility=View.VISIBLE
//                    holder.mStatueTV.text=""
//                }
//            }
            holder.mHandlerBTN.visibility=View.GONE
            if (mViewModel?.searchMine?.value == true) {
                holder.mFinishRL.visibility=View.VISIBLE
                holder.mHandlerBTN.visibility = View.VISIBLE
                holder.mHandlerBTN.text = "编辑"
            }
//          散件过磅
            holder.mHandlerBTN.setOnClickListener {
                if (type==1&&data.isEditable==1){
                    //我录入的可以重新编辑
                    context.startActivity<PartInputActivity>(MyConstants.ORDER_ID to data.orderId)
                }else{
                    val mOrderIdList=ArrayList<Int>()
                    mOrderIdList.add( data.orderId)
                    context.startActivity<PartWeightActivity>(
                        MyConstants.POSITION to position,
                        MyConstants.ORDER_ID to data.orderId,
                        MyConstants.ORDER_ID_LIST to getGson().toJson(mOrderIdList))
                }
            }
            holder.mParentLL.setOnClickListener {
                context.startActivity<PartInWerehouseDetailActivity>(MyConstants.ORDER_ID to data.orderId)
            }
            //holder.mDeleteBTN.visibility=if(data.isRemove==1)View.VISIBLE else View.GONE
            holder.mDeleteBTN.visibility= View.GONE
            holder.mDeleteBTN.setOnClickListener {
                listener.itemDelete(position,data)
            }
        }
    }
    interface PartInWarehouseCallBack{
        fun itemDelete(position:Int,data:PartInWarehouseEntity.DataBean.ListBean)
    }
    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val mOrderNumberTV: TextView = view.findViewById(R.id.mOrderNumberTV)
//        val mStatueTV: TextView = view.findViewById(R.id.mStatueTV)
        val mNameTV: TextView = view.findViewById(R.id.mNameTV)
        val mTimeTV: TextView = view.findViewById(R.id.mTimeTV)
        val mPartTV: TextView = view.findViewById(R.id.mPartTV)
        val mPartFromTV: TextView = view.findViewById(R.id.mPartFromTV)
        val mPartCountTV: TextView = view.findViewById(R.id.mPartCountTV)
        val mFinishRL: RelativeLayout = view.findViewById(R.id.mFinishRL)
        val mHandlerBTN: Button = view.findViewById(R.id.mHandlerBTN)
        val mView: View = view.findViewById(R.id.mView)
        val mParentLL: LinearLayoutCompat = view.findViewById(R.id.mParentLL)
        val mCheckBox:CheckBox=view.findViewById(R.id.mCheckBox)
        val mDeleteBTN:Button=view.findViewById(R.id.mDeleteBTN)
    }
}